import axios from 'axios';

// 设置接口超时时间
axios.defaults.timeout = 60000;
// 设置 post、get默认 Content-Type
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
axios.defaults.headers.get['Content-Type'] = 'application/json;charset=UTF-8'
axios.defaults.headers.post['X-Content-Type-Options'] = 'nosniff'
axios.defaults.headers.get['X-Content-Type-Options'] = 'nosniff'
// 请求地址，这里是动态赋值的的环境变量，下一篇会细讲，这里跳过
// @ts-ignore
// axios.defaults.baseURL = import.meta.env.VITE_API_DOMAIN;
axios.defaults.baseURL = '/';

// 封装 GET POST 请求并导出
export function request(url = '', params = {}, type = 'POST') {
    //设置 url params type 的默认值
    return new Promise((resolve, reject) => {
        let promise: any
        if (type.toUpperCase() === 'GET') {
            promise = axios({
                url,
                params
            })
        } else if (type.toUpperCase() === 'POST') {
            promise = axios({
                method: 'POST',
                url,
                data: params
            })
        }
        //处理返回
        promise.then((res: any) => {
            resolve(res)
        }).catch((err: any) => {
            reject(err)
        })
    })
}
